﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using Yule8xCommon.Applib;

namespace Yule8xWxToolkit.Other
{
    public partial class NickNames : System.Web.UI.Page
    {
        protected int count;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                tb_content.Text = RuntimeDataUtil.GetNickNameText();
                //tb_content.Text = appendCity();
                count = tb_content.Text.Split(Environment.NewLine.ToCharArray(), StringSplitOptions.RemoveEmptyEntries).Length;
            }

        }
        protected string appendCity()
        {
            string[] citytext = File.ReadAllLines(Server.MapPath("/Runtime/AppData/city.txt"));
            IList<string> newnames = new List<string>();
            string[] names = tb_content.Text.Split(Environment.NewLine.ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
            Random rnd = new Random();
            string city;
            int len = citytext.Length-1;
            foreach (string name in names)
            {
                city = citytext[rnd.Next(len)];
                newnames.Add(city+" "+name);
            }
            return string.Join(Environment.NewLine, newnames);
        }


        protected void btn_save_Click(object sender, EventArgs e)
        {
            try
            {
                File.WriteAllText(RuntimeDataUtil.filePathes.nicknames_file, tb_content.Text);
            }
            catch (Exception ex)
            {
                Response.Write(ex.Message);
                Response.End();
            }
            Response.Redirect("NickNames.aspx?result=success");
        }
    }
}